
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>RxCollection · RxDB - Documentation</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
        <link rel="stylesheet" href="styles/website.css">
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="rx-collection.html" />
    
    
    <link rel="prev" href="rx-schema.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="https://twitter.com/rxdbjs" target="_blank" class="custom-link">Follow @twitter </a>
        </li>
    
        
        <li>
            <a href="https://gitter.im/pubkey/rxdb" target="_blank" class="custom-link">Chat @gitter </a>
        </li>
    
        
        <li>
            <a href="https://github.com/pubkey/rxdb" target="_blank" class="custom-link">Star @github </a>
        </li>
    
        
        <li>
            <a href="https://www.patreon.com/rxdb/" target="_blank" class="custom-link">Donate @patreon </a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="install.html">
            
                <a href="install.html">
            
                    
                    Install
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="install.html">
            
                <a href="install.html#npm">
            
                    
                    npm
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="install.html">
            
                <a href="install.html#import">
            
                    
                    import
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="rx-database.html">
            
                <a href="rx-database.html">
            
                    
                    RxDatabase
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="rx-database.html">
            
                <a href="rx-database.html#creation">
            
                    
                    Creation
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1.1" data-path="rx-database.html">
            
                <a href="rx-database.html#name">
            
                    
                    name
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.2" data-path="rx-database.html">
            
                <a href="rx-database.html#adapter">
            
                    
                    adapter
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.3" data-path="rx-database.html">
            
                <a href="rx-database.html#password">
            
                    
                    password
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.4" data-path="rx-database.html">
            
                <a href="rx-database.html#multiinstance">
            
                    
                    multiInstance
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.5" data-path="rx-database.html">
            
                <a href="rx-database.html#eventreduce">
            
                    
                    eventReduce
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.6" data-path="rx-database.html">
            
                <a href="rx-database.html#ignoreduplicate">
            
                    
                    ignoreDuplicate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.7" data-path="rx-database.html">
            
                <a href="rx-database.html#pouchSettings">
            
                    
                    pouchSettings
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="rx-database.html">
            
                <a href="rx-database.html#functions">
            
                    
                    Functions
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.2.1" data-path="rx-database.html">
            
                <a href="rx-database.html#observe-with-">
            
                    
                    $
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.2" data-path="rx-database.html">
            
                <a href="rx-database.html#dump">
            
                    
                    dump()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.3" data-path="rx-database.html">
            
                <a href="rx-database.html#importdump">
            
                    
                    importDump()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.4" data-path="rx-database.html">
            
                <a href="rx-database.html#server">
            
                    
                    server()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.5" data-path="rx-database.html">
            
                <a href="rx-database.html#waitforleadership">
            
                    
                    waitForLeadership()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.6" data-path="rx-database.html">
            
                <a href="rx-database.html#requestidlepromise">
            
                    
                    requestIdlePromise()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.7" data-path="rx-database.html">
            
                <a href="rx-database.html#destroy">
            
                    
                    destroy()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.8" data-path="rx-database.html">
            
                <a href="rx-database.html#remove">
            
                    
                    remove()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.9" data-path="rx-database.html">
            
                <a href="rx-database.html#checkadapter">
            
                    
                    checkAdapter()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2.10" data-path="rx-database.html">
            
                <a href="rx-database.html#isrxdatabase">
            
                    
                    isRxDatabase()
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="rx-schema.html">
            
                <a href="rx-schema.html">
            
                    
                    RxSchema
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="rx-schema.html">
            
                <a href="rx-schema.html#example">
            
                    
                    Example
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="rx-schema.html">
            
                <a href="rx-schema.html#create-a-collection-with-the-schema">
            
                    
                    Create a collection with the schema
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="rx-schema.html">
            
                <a href="rx-schema.html#version">
            
                    
                    version
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="rx-schema.html">
            
                <a href="rx-schema.html#keycompression">
            
                    
                    keyCompression
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="rx-schema.html">
            
                <a href="rx-schema.html#indexes">
            
                    
                    indexes
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="rx-schema.html">
            
                <a href="rx-schema.html#attachments">
            
                    
                    attachments
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="rx-schema.html">
            
                <a href="rx-schema.html#default">
            
                    
                    default
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="rx-schema.html">
            
                <a href="rx-schema.html#final">
            
                    
                    final
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="rx-schema.html">
            
                <a href="rx-schema.html#encryption">
            
                    
                    encryption
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter active" data-level="1.5" data-path="rx-collection.html">
            
                <a href="rx-collection.html">
            
                    
                    RxCollection
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="rx-collection.html">
            
                <a href="rx-collection.html#creating-a-collection">
            
                    
                    Creation
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1.1" data-path="rx-collection.html">
            
                <a href="rx-collection.html#name">
            
                    
                    name
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.2" data-path="rx-collection.html">
            
                <a href="rx-collection.html#schema">
            
                    
                    schema
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.3" data-path="rx-collection.html">
            
                <a href="rx-collection.html#pouchSettings">
            
                    
                    pouchSettings
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.4" data-path="rx-collection.html">
            
                <a href="rx-collection.html#orm-functions">
            
                    
                    ORM-functions
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.5" data-path="rx-collection.html">
            
                <a href="rx-collection.html#Migration">
            
                    
                    Migration
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="rx-collection.html">
            
                <a href="rx-collection.html#functions">
            
                    
                    Functions
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.2.1" data-path="rx-collection.html">
            
                <a href="rx-collection.html#observe-">
            
                    
                    $
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.2" data-path="rx-collection.html">
            
                <a href="rx-collection.html#insert">
            
                    
                    insert()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.3" data-path="rx-collection.html">
            
                <a href="rx-collection.html#newdocument">
            
                    
                    newDocument()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.4" data-path="rx-collection.html">
            
                <a href="rx-collection.html#upsert">
            
                    
                    upsert()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.5" data-path="rx-collection.html">
            
                <a href="rx-collection.html#atomicupsert">
            
                    
                    atomicUpsert()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.6" data-path="rx-collection.html">
            
                <a href="rx-collection.html#find">
            
                    
                    find()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.7" data-path="rx-collection.html">
            
                <a href="rx-collection.html#findone">
            
                    
                    findOne()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.8" data-path="rx-collection.html">
            
                <a href="rx-collection.html#findByIds">
            
                    
                    findByIds()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.9" data-path="rx-collection.html">
            
                <a href="rx-collection.html#dump">
            
                    
                    dump()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.10" data-path="rx-collection.html">
            
                <a href="rx-collection.html#importdump">
            
                    
                    importDump()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.11" data-path="rx-collection.html">
            
                <a href="rx-collection.html#sync">
            
                    
                    sync()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.12" data-path="rx-collection.html">
            
                <a href="rx-collection.html#remove">
            
                    
                    remove()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.13" data-path="rx-collection.html">
            
                <a href="rx-collection.html#destroy">
            
                    
                    destroy()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2.14" data-path="rx-collection.html">
            
                <a href="rx-collection.html#isrxcollection">
            
                    
                    isRxCollection()
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="rx-document.html">
            
                <a href="rx-document.html">
            
                    
                    RxDocument
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="rx-document.html">
            
                <a href="rx-document.html#insert">
            
                    
                    Insert
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="rx-document.html">
            
                <a href="rx-document.html#find">
            
                    
                    Find
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="rx-document.html">
            
                <a href="rx-document.html#functions">
            
                    
                    Functions
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.3.1" data-path="rx-document.html">
            
                <a href="rx-document.html#get">
            
                    
                    get()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3.2" data-path="rx-document.html">
            
                <a href="rx-document.html#get$">
            
                    
                    get$()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3.3" data-path="rx-document.html">
            
                <a href="rx-document.html#proxy-get">
            
                    
                    proxy-get
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3.4" data-path="rx-document.html">
            
                <a href="rx-document.html#update">
            
                    
                    update()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3.5" data-path="rx-document.html">
            
                <a href="rx-document.html#atomicupdate">
            
                    
                    atomicUpdate()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3.6" data-path="rx-document.html">
            
                <a href="rx-document.html#atomicset">
            
                    
                    atomicSet()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3.7" data-path="rx-document.html">
            
                <a href="rx-document.html#observe-">
            
                    
                    $
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3.8" data-path="rx-document.html">
            
                <a href="rx-document.html#remove">
            
                    
                    remove()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3.9" data-path="rx-document.html">
            
                <a href="rx-document.html#deleted$">
            
                    
                    deleted$
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3.10" data-path="rx-document.html">
            
                <a href="rx-document.html#tojson">
            
                    
                    toJSON()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3.11" data-path="rx-document.html">
            
                <a href="rx-document.html#set">
            
                    
                    set()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3.12" data-path="rx-document.html">
            
                <a href="rx-document.html#save">
            
                    
                    save()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3.13" data-path="rx-document.html">
            
                <a href="rx-document.html#isrxdocument">
            
                    
                    isRxDocument()
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="rx-query.html">
            
                <a href="rx-query.html">
            
                    
                    RxQuery
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="rx-query.html">
            
                <a href="rx-query.html#find">
            
                    
                    find()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="rx-query.html">
            
                <a href="rx-query.html#findOne">
            
                    
                    findOne()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="rx-query.html">
            
                <a href="rx-query.html#exec">
            
                    
                    exec()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="rx-query.html">
            
                <a href="rx-query.html#observe-">
            
                    
                    $
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="rx-query.html">
            
                <a href="rx-query.html#update">
            
                    
                    update()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6" data-path="rx-query.html">
            
                <a href="rx-query.html#remove">
            
                    
                    remove()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.7" data-path="rx-query.html">
            
                <a href="rx-query.html#doesDocumentDataMatch">
            
                    
                    doesDocumentDataMatch()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.8" data-path="rx-query.html">
            
                <a href="rx-query.html#examples">
            
                    
                    Examples
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.9" data-path="rx-query.html">
            
                <a href="rx-query.html#isrxquery">
            
                    
                    isRxQuery()
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="rx-attachment.html">
            
                <a href="rx-attachment.html">
            
                    
                    RxAttachment
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="middleware.html">
            
                <a href="middleware.html">
            
                    
                    Middleware-hooks
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="orm.html">
            
                <a href="orm.html">
            
                    
                    ORM/DRM
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="population.html">
            
                <a href="population.html">
            
                    
                    Population
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="data-migration.html">
            
                <a href="data-migration.html">
            
                    
                    DataMigration
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="leader-election.html">
            
                <a href="leader-election.html">
            
                    
                    LeaderElection
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="replication.html">
            
                <a href="replication.html">
            
                    
                    Replication CouchDB
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="replication-graphql.html">
            
                <a href="replication-graphql.html">
            
                    
                    Replication GraphQL
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="in-memory.html">
            
                <a href="in-memory.html">
            
                    
                    InMemory
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="query-cache.html">
            
                <a href="query-cache.html">
            
                    
                    QueryCache
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="rx-local-document.html">
            
                <a href="rx-local-document.html">
            
                    
                    LocalDocuments
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="custom-build.html">
            
                <a href="custom-build.html">
            
                    
                    Custom Build
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="plugins.html">
            
                <a href="plugins.html">
            
                    
                    Creating Plugins
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="adapters.html">
            
                <a href="adapters.html">
            
                    
                    Adapters
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" >
            
                <span>
            
                    
                    Tutorials
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.22.1" data-path="tutorials/typescript.html">
            
                <a href="tutorials/typescript.html">
            
                    
                    Use RxDB with Typescript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.2" data-path="tutorials/server.html">
            
                <a href="tutorials/server.html">
            
                    
                    Using the Server Plugin
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.23" data-path="questions-answers.html">
            
                <a href="questions-answers.html">
            
                    
                    Questions & Answers
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.24" data-path="contribute.html">
            
                <a href="contribute.html">
            
                    
                    Contribute
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >RxCollection</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="rxcollection">RxCollection</h1>
<p>A collection stores documents of the same type.</p>
<h2 id="creating-a-collection">Creating a Collection</h2>
<p>To create a collection you need a RxDatabase object which has the .collection()-method. Every collection needs a collection name and a valid RxSchema. Other attributes are optional.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> myCollection = <span class="hljs-keyword">await</span> myDatabase.collection({
  name: <span class="hljs-string">&apos;humans&apos;</span>,
  schema: mySchema,
  pouchSettings: {} <span class="hljs-comment">// (optional)</span>
  statics: {}, <span class="hljs-comment">// (optional) // ORM-functions for this collection</span>
  methods: {}, <span class="hljs-comment">// (optional) ORM-functions for documents</span>
  attachments: {}, <span class="hljs-comment">// (optional) ORM-functions for attachments</span>
  options: {}, <span class="hljs-comment">// (optional) Custom paramters that might be used in plugins</span>
  migrationStrategies: {}, <span class="hljs-comment">// (optional)</span>
  autoMigrate: <span class="hljs-literal">true</span>, <span class="hljs-comment">// (optional)</span>
  cacheReplacementPolicy: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{}, <span class="hljs-comment">// (optional) custoom cache replacement policy</span>
});
</code></pre>
<h3 id="name">name</h3>
<p>The name uniquely identifies the collection and should be used to refind the collection in the database. Two different collections in the same database can never have the same name. Collection names must match the following regex: <code>^[a-z][a-z0-9]*$</code>.</p>
<h3 id="schema">schema</h3>
<p>The schema defines how your data looks and how it should be handled. You can pass a RxSchema object or a simple javascript-object from which the schema will be generated.</p>
<h3 id="pouchsettings">pouchSettings</h3>
<p>You can pass settings directly to the <a href="https://pouchdb.com/api.html#options" target="_blank">pouchdb database create options</a> through this property.</p>
<h3 id="orm-functions">ORM-functions</h3>
<p>With the parameters <code>statics</code>, <code>methods</code> and <code>attachments</code>, you can defined ORM-functions that are applied to each of these objects that belong to this collection. See <a href="orm.html">ORM/DRM</a>.</p>
<h3 id="migration">Migration</h3>
<p>With the parameters <code>migrationStrategies</code> and <code>autoMigrate</code> you can specify how mirgration between different schema-versions should be done. <a href="data-migration.html">See Migration</a>.</p>
<h2 id="get-a-collection-from-the-database">Get a collection from the database</h2>
<p>To get an existing collection from the database, call the collection name directly on the database:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// newly created collection</span>
<span class="hljs-keyword">const</span> collection = <span class="hljs-keyword">await</span> db.collection({
  name: <span class="hljs-string">&apos;heroes&apos;</span>,
  schema: mySchema
});
<span class="hljs-keyword">const</span> collection2 = db.heroes;
<span class="hljs-built_in">console</span>.log(collection === collection2); <span class="hljs-comment">//&gt; true</span>
</code></pre>
<h2 id="functions">Functions</h2>
<h3 id="observe-">Observe $</h3>
<p>Calling this will return an <a href="http://reactivex.io/rxjs/manual/overview.html#observable" target="_blank">rxjs-Observable</a> which streams every change to data of this collection.</p>
<pre><code class="lang-js">myCollection.$.subscribe(changeEvent =&gt; <span class="hljs-built_in">console</span>.dir(changeEvent));

<span class="hljs-comment">// you can also observe single event-types with insert$ update$ remove$</span>
myCollection.insert$.subscribe(changeEvent =&gt; <span class="hljs-built_in">console</span>.dir(changeEvent));
myCollection.update$.subscribe(changeEvent =&gt; <span class="hljs-built_in">console</span>.dir(changeEvent));
myCollection.remove$.subscribe(changeEvent =&gt; <span class="hljs-built_in">console</span>.dir(changeEvent));
</code></pre>
<h3 id="insert">insert()</h3>
<p>Use this to insert new documents into the database. The collection will validate the schema and automatically encrypt any encrypted fields. Returns the new RxDocument.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> doc = <span class="hljs-keyword">await</span> myCollection.insert({
  name: <span class="hljs-string">&apos;foo&apos;</span>,
  lastname: <span class="hljs-string">&apos;bar&apos;</span>
});
</code></pre>
<h3 id="bulkinsert">bulkInsert()</h3>
<p>When you have to insert many documents at once, use bulk insert. This is much faster then calling <code>.insert()</code> multiple times.
Returns an object with a <code>success</code>- and <code>error</code>-array.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> result = <span class="hljs-keyword">await</span> myCollection.bulkInsert([{
  name: <span class="hljs-string">&apos;foo1&apos;</span>,
  lastname: <span class="hljs-string">&apos;bar1&apos;</span>
},
{
  name: <span class="hljs-string">&apos;foo2&apos;</span>,
  lastname: <span class="hljs-string">&apos;bar2&apos;</span>
}]);

<span class="hljs-comment">// &gt; {</span>
<span class="hljs-comment">//   success: [RxDocument, RxDocument],</span>
<span class="hljs-comment">//   error: []</span>
<span class="hljs-comment">// }</span>
</code></pre>
<p>NOTICE: <code>bulkInsert</code> will not fail on update conflicts and you cannot expect that on failure the other documents are not inserted.</p>
<h3 id="newdocument">newDocument()</h3>
<p>Sometimes it can be helpful to spawn and use documents before saving them into the database.
This is useful especially when you want to use the ORM methods or prefill values from form data.
You can create temporary documents by calling <code>RxCollection.newDocument(initalData)</code>.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> tempDoc = myCollection.newDocument({
    firstName: <span class="hljs-string">&apos;Bob&apos;</span>
});

<span class="hljs-comment">// fill in data later</span>
tempDoc.lastName = <span class="hljs-string">&apos;Kelso&apos;</span>;
tempDoc.age = <span class="hljs-number">77</span>;

<span class="hljs-comment">// saving a temporary document will transform it to a standard RxDocument</span>
<span class="hljs-keyword">await</span> tempDoc.save();
</code></pre>
<h3 id="upsert">upsert()</h3>
<p>Inserts the document if it does not exist within the collection, otherwise it will overwrite it. Returns the new or overwritten RxDocument.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> doc = <span class="hljs-keyword">await</span> myCollection.upsert({
  name: <span class="hljs-string">&apos;foo&apos;</span>,
  lastname: <span class="hljs-string">&apos;bar2&apos;</span>
});
</code></pre>
<h3 id="atomicupsert">atomicUpsert()</h3>
<p>When you run many upsert operations on the same RxDocument in a very short timespan, you might get a <code>409 Conflict</code> error.
This means that you tried to run a <code>.upsert()</code> on the document, while the previous upsert operation was still running.
To prevent these types of errors, you can run atomic upsert operations.
The behavior is similar to <a href="rx-document.html#atomicUpdate">RxDocument.atomicUpdate</a>.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> docData = {
    name: <span class="hljs-string">&apos;Bob&apos;</span>, <span class="hljs-comment">// primary</span>
    lastName: <span class="hljs-string">&apos;Kelso&apos;</span>
};

myCollection.upsert(docData);
myCollection.upsert(docData);
<span class="hljs-comment">// -&gt; throws because of parrallel update to the same document</span>

myCollection.atomicUpsert(docData);
myCollection.atomicUpsert(docData);
myCollection.atomicUpsert(docData);

<span class="hljs-comment">// wait until last upsert finished</span>
<span class="hljs-keyword">await</span> myCollection.atomicUpsert(docData);
<span class="hljs-comment">// -&gt; works</span>
</code></pre>
<h3 id="find">find()</h3>
<p>To find documents in your collection, use this method. <a href="rx-query.html#find">See RxQuery.find()</a>.</p>
<pre><code class="lang-js"><span class="hljs-comment">// find all that are older then 18</span>
<span class="hljs-keyword">const</span> olderDocuments = <span class="hljs-keyword">await</span> myCollection
    .find()
    .where(<span class="hljs-string">&apos;age&apos;</span>)
    .gt(<span class="hljs-number">18</span>)
    .exec(); <span class="hljs-comment">// execute</span>
</code></pre>
<h3 id="findone">findOne()</h3>
<p>This does basically what find() does, but it returns only a single document. You can pass a primary value to find a single document more easily.</p>
<p>To find documents in your collection, use this method. <a href="rx-query.html#findOne">See RxQuery.find()</a>.</p>
<pre><code class="lang-js"><span class="hljs-comment">// get document with name:foobar</span>
myCollection.findOne().where(<span class="hljs-string">&apos;name&apos;</span>).eq(<span class="hljs-string">&apos;foo&apos;</span>)
  .exec().then(doc =&gt; <span class="hljs-built_in">console</span>.dir(doc));

<span class="hljs-comment">// get document by primary, functionally identical to above query</span>
myCollection.findOne(<span class="hljs-string">&apos;foo&apos;</span>)
  .exec().then(doc =&gt; <span class="hljs-built_in">console</span>.dir(doc));
</code></pre>
<h3 id="findbyids">findByIds()</h3>
<p>Notice: This method is in beta and might be changed without notice.</p>
<p>Find many documents by their id (primary value). This has a way better performance than running multiple <code>findOne()</code> or a <code>find()</code> with a big <code>$or</code> selector.</p>
<p>Returns a <code>Map</code> where the primary key of the document is mapped to the document. Documents that do not exist or are deleted, will not be inside of the returned Map.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> ids = [
  <span class="hljs-string">&apos;alice&apos;</span>,
  <span class="hljs-string">&apos;bob&apos;</span>,
  <span class="hljs-comment">/* ... */</span>
];
<span class="hljs-keyword">const</span> docsMap = <span class="hljs-keyword">await</span> myCollection.findByIds(ids);

<span class="hljs-built_in">console</span>.dir(docsMap); <span class="hljs-comment">// Map(2)</span>
</code></pre>
<h3 id="dump">dump()</h3>
<p>Use this function to create a json export from every document in the collection. You can pass true as parameter to decrypt the encrypted data fields of your documents.</p>
<pre><code class="lang-js">myCollection.dump()
  .then(json =&gt; <span class="hljs-built_in">console</span>.dir(json));

<span class="hljs-comment">// decrypted dump</span>
myCollection.dump(<span class="hljs-literal">true</span>)
  .then(json =&gt; <span class="hljs-built_in">console</span>.dir(json));
</code></pre>
<h3 id="importdump">importDump()</h3>
<p>To import the json dump into your collection, use this function.</p>
<pre><code class="lang-js"><span class="hljs-comment">// import the dump to the database</span>
myCollection.importDump(json)
  .then(() =&gt; <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;done&apos;</span>));
</code></pre>
<h3 id="sync">sync()</h3>
<p>This method allows you to replicate data between other RxCollections, pouchdb instances or remote servers which support the couchdb-sync-protocol.
Full documentation on how to use replication is <a href="replication.html">here</a>.</p>
<h3 id="remove">remove()</h3>
<p>Removes all known data of the collection and its previous versions.
This removes the documents, the schemas, and older schemaVersions.</p>
<pre><code class="lang-js"><span class="hljs-keyword">await</span> myCollection.remove();
<span class="hljs-comment">// collection is now removed and can be re-created</span>
</code></pre>
<h3 id="destroy">destroy()</h3>
<p>Destroys the collection&apos;s object instance. This is to free up memory and stop all observings and replications.</p>
<pre><code class="lang-js"><span class="hljs-keyword">await</span> myCollection.destroy();
</code></pre>
<h3 id="isrxcollection">isRxCollection</h3>
<p>Returns true if the given object is an instance of RxCollection. Returns false if not.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> is = isRxCollection(myObj);
</code></pre>
<hr>
<p>If you are new to RxDB, you should continue <a href="rx-document.html">here</a></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="rx-schema.html#encryption" class="navigation navigation-prev " aria-label="Previous page: encryption">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="rx-collection.html#creating-a-collection" class="navigation navigation-next " aria-label="Next page: Creation">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"RxCollection","level":"1.5","depth":1,"next":{"title":"Creation","level":"1.5.1","depth":2,"anchor":"#creating-a-collection","path":"rx-collection.md","ref":"./rx-collection.md#creating-a-collection","articles":[{"title":"name","level":"1.5.1.1","depth":3,"anchor":"#name","path":"rx-collection.md","ref":"./rx-collection.md#name","articles":[]},{"title":"schema","level":"1.5.1.2","depth":3,"anchor":"#schema","path":"rx-collection.md","ref":"./rx-collection.md#schema","articles":[]},{"title":"pouchSettings","level":"1.5.1.3","depth":3,"anchor":"#pouchSettings","path":"rx-collection.md","ref":"./rx-collection.md#pouchSettings","articles":[]},{"title":"ORM-functions","level":"1.5.1.4","depth":3,"anchor":"#orm-functions","path":"rx-collection.md","ref":"./rx-collection.md#orm-functions","articles":[]},{"title":"Migration","level":"1.5.1.5","depth":3,"anchor":"#Migration","path":"rx-collection.md","ref":"./rx-collection.md#Migration","articles":[]}]},"previous":{"title":"encryption","level":"1.4.9","depth":2,"anchor":"#encryption","path":"rx-schema.md","ref":"./rx-schema.md#encryption","articles":[]},"dir":"ltr"},"config":{"plugins":["edit-link","github","custom-favicon","-sharing","expandable-chapters","scripts"],"root":".","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"github":{"url":"https://github.com/pubkey/rxdb"},"scripts":{"files":["./gitter.js"]},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"favicon":"./files/logo/icon.ico","custom-favicon":{},"edit-link":{"label":"Edit This Page","base":"https://github.com/pubkey/rxdb/tree/master/docs-src"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"RxDB - Documentation","links":{"sidebar":{"Follow @twitter ":"https://twitter.com/rxdbjs","Chat @gitter ":"https://gitter.im/pubkey/rxdb","Star @github ":"https://github.com/pubkey/rxdb","Donate @patreon ":"https://www.patreon.com/rxdb/"}},"gitbook":"3.2.3"},"file":{"path":"rx-collection.md","mtime":"2020-07-22T22:17:56.213Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-08-02T14:19:21.945Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-edit-link/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-scripts/c6361af027a93a41d58227852f7ae965-gitter.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

